﻿using System;
using System.Web.UI;
using WeiboFilter.DAL;
using WeiboFilter.Models;
using WeiboFilter.SinaWeibo;

namespace WeiboFilter
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["oauth_token"] == null && Session["customer"] == null)
            {
                Response.Write("<script>alert('请先登录！');window.location.href='Login.aspx';</script>");
            }
            if (!Page.IsPostBack)
            {
                oAuthSina oauth = new oAuthSina();
                if (Request["oauth_verifier"] != null)
                {//oauth_verifier研究
                    oauth.token = Session["oauth_token"].ToString();
                    oauth.tokenSecret = Session["oauth_token_secret"].ToString();
                    oauth.Verifier = Request["oauth_verifier"].ToString();
                    oauth.AccessTokenGet();
                    Session["oauth_token"] = oauth.token;
                    Session["oauth_token_secret"] = oauth.tokenSecret;
                    Response.Redirect("/Default.aspx");//这个对页面中数据的危害！！
                }
                if (Session["oauth_token"] != null)
                {
                    GetDataFromSina gd = new GetDataFromSina(Session["oauth_token"].ToString(), Session["oauth_token_secret"].ToString());
                    User u = gd.getLoginUser();
                    imgHead.ImageUrl = u.profile_image_url;
                    lblLoginType.Text = "在线模式";
                    //注册账户
                    CustomerDAL cD = new CustomerDAL();
                    Customer c = cD.InsertCustomer(u);
                    Session["customer"] = c.c_id;
                }
            }
        }
    }
}